package com.bw.mvvm_common.mvvm.view;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.LogUtils;
import com.bw.mvvm_common.field.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/12 0012 下午 2:25
 *
 */
abstract class BaseActivity <V extends ViewDataBinding, VM extends BaseViewModel>extends
        AppCompatActivity implements IView {
    protected V v;
    protected VM vm;

    @Override
    protected void onCreate(  Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewModel();

        getLifecycle().addObserver(vm);
        vm.getUiChange().getHideDialogObservable().observe(this,hideDialogObserver);
        vm.getUiChange().getFinishActObservable().observe(this,finishObserver);
        vm.getUiChange().getShowDialogObservable().observe(this,showDialogObserver);
        vm.getUiChange().getShowMsgObservable().observe(this,showMsgObserver);
        vm.getUiChange().getStartPageObservable().observe(this,startPageObserver);

        bindDataBinding();
        v.setVariable(initVerable(),vm);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        vm.getUiChange().getHideDialogObservable().observe(this,hideDialogObserver);
        vm.getUiChange().getFinishActObservable().observe(this,finishObserver);
        vm.getUiChange().getShowDialogObservable().observe(this,showDialogObserver);
        vm.getUiChange().getShowMsgObservable().observe(this,showMsgObserver);
        vm.getUiChange().getStartPageObservable().observe(this,startPageObserver);
        getLifecycle().removeObserver(vm);
        v.unbind();

    }

    @Override
    public void initViewModel() {
        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
        Type[] types = parameterizedType.getActualTypeArguments();
        Class clazz = (Class) types[1];
        vm= (VM) new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(clazz);

    }

    @Override
    public void bindDataBinding() {
        v = DataBindingUtil.setContentView(this,bindLayout());
    }

    @Override
    public void showDialog() {
        LogUtils.e("showDialog");

    }

    @Override
    public void hideDialog() {
        LogUtils.e("hideDialog");
    }

    @Override
    public void showMsg(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void finishpage() {
        finish();
    }

    @Override
    public void startPage(Class clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (bundle!=null){
            intent.putExtra(UIField.PAGEBUNDLE,bundle);
        }
        startActivity(intent);
    }

    private Observer<String> showDialogObserver = new Observer<String>() {
        @Override
        public void onChanged(String s) {
            showDialog();
        }
    };
    private Observer<String>hideDialogObserver = new Observer<String>(){
        @Override
        public void onChanged( String o) {
            hideDialog();
        }
    };
    private Observer<String>finishObserver = new Observer<String>(){
        @Override
        public void onChanged( String o) {
            finishpage();
        }
    };
    private Observer<String>showMsgObserver = new Observer<String>(){
        @Override
        public void onChanged( String o) {
            showMsg(o);
        }
    };
    private Observer<HashMap<String,Object>>startPageObserver = new Observer<HashMap<String, Object>>() {
        @Override
        public void onChanged( HashMap<String, Object> map) {
            Class clazz = (Class) map.get(UIField.PAGECLAZZ);
            Object o = map.get(UIField.PAGEBUNDLE);
            Bundle bundle = null;
            if (o!=null)
                bundle = (Bundle) o;
            startPage(clazz,bundle);
        }
    };



}
